package cn.mifengkong.appcommon.liveness;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import com.mifengkong.frtools.util.FRFileUtils;
import com.oliveapp.face.livenessdetectorsdk.livenessdetector.datatype.LivenessDetectionFrames;
import com.oliveapp.face.livenessdetectorsdk.livenessdetector.datatype.OliveappFaceInfo;
import com.oliveapp.libcommon.utility.PackageNameManager;

import cn.mifengkong.appcommon.liveness.view_controller.LivenessDetectionMainActivity;

import static cn.mifengkong.appcommon.liveness.ILivenessHelper.LIVENESSACTIVITYSUCCESSTAG;


/**
 * 样例活体检测Activity
 */
public class SampleLivenessActivity extends LivenessDetectionMainActivity {

    public static final String TAG = SampleLivenessActivity.class.getSimpleName();

    private byte[] mPackageByteArray;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 如果有设置全局包名的需要, 在这里进行设置
        PackageNameManager.setPackageName(getPackageName());
        super.onCreate(savedInstanceState);
    }

    ////////////// INITIALIZATION //////////////
    @Override
    public void onInitializeSucc() {
        super.onInitializeSucc();
        super.startVerification();
    }

    @Override
    public void onInitializeFail(Throwable e) {
        super.onInitializeFail(e);
        Log.e(TAG, "无法初始化活体检测...", e);
        Toast.makeText(this, "无法初始化活体检测", Toast.LENGTH_LONG).show();
        getReturnData(false);
    }

    ////////////// LIVENESS DETECTION /////////////////


    private void getReturnData(final boolean flag) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent();
                i.putExtra(LIVENESSACTIVITYSUCCESSTAG, flag);
                handleLivenessFinish(i);
            }
        }, 0);
    }

    @Override
    public void onLivenessFail(int result, LivenessDetectionFrames livenessDetectionFrames) {
        super.onLivenessFail(result, livenessDetectionFrames);
        getReturnData(false);
    }

    private void handleLivenessFinish(Intent i) {
        setResult(1, i);
        finish();
    }

    /**===================================活体检测算法的回调==================================**/
    /**
     * 活体检测成功的回调，同时获取大礼包
     * 成功回调的接口在1.7.a_hotfix被拆成了2步，不再使用该接口
     *
     * @param livenessDetectionFrames 活体检测抓取的图片
     * @param faceInfo                捕获到的人脸信息
     */
    @Override
    public void onLivenessSuccess(final LivenessDetectionFrames livenessDetectionFrames, OliveappFaceInfo faceInfo) {
        // do nothing
    }

    @Override
    public void onLivenessSuccess(OliveappFaceInfo oliveappFaceInfo) {
        super.onLivenessSuccess(null, oliveappFaceInfo);
        CheckImagePackage checkImagePackage = new CheckImagePackage();
        checkImagePackage.execute();
    }


    /**
     * 获取大礼包并访问私有云的样例代码，集成时请不要使用
     */
    private class CheckImagePackage extends AsyncTask<Void, Void, Integer> {

        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(SampleLivenessActivity.this,
                    null, "正在比对，请稍等", true, false);
        }

        @Override
        protected Integer doInBackground(Void... voids) {
            // 获取大礼包，耗时操作，不可在UI线程执行
            LivenessDetectionFrames pkg = getLivenessDetectionPackage();
            if (pkg == null) {
                return null;
            }
            mPackageByteArray = pkg.verificationData;
            String base64Data1 = Base64.encodeToString(mPackageByteArray, Base64.NO_WRAP);
            FRFileUtils.saveWithRepalce("base64Data1", base64Data1);
            return mPackageByteArray.length;
        }

        @Override
        protected void onPostExecute(Integer resultLenght) {
            mProgressDialog.dismiss();
            if (resultLenght != 0) {
                getReturnData(true);
            } else {
                getReturnData(true);
            }
        }
    }

}
